package com.css.fxfzypg.logInfo.repository.impl;

import com.css.fxfzypg.logInfo.entity.SLog;
import com.css.fxfzypg.logInfo.repository.SyslogReqository;
import com.css.fxfzypg.util.PlatformObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class SyslogReqositoryImpl implements SyslogReqository {

    @Autowired
    JdbcTemplate jdbcTemplate;
    @Autowired
    NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    @Override
    @Transactional
    public boolean saveLogs(SLog log) {

        String sql = "INSERT INTO public.sys_log (log_id, op_id, op_name, op_ip, operator_type, " +
                "op_time, sys_id, func_id, event_type, op_type, op_obj_type, op_obj_id, rel_obj_type, " +
                "rel_obj_id, server_ip, server_name, op_result, log_level, log_data, log_memo, " +
                "unit_id, op_client_info, op_os, op_explorer, op_way, sys_name, user_type, " +
                "insert_time, province_id) " +
                "VALUES (:logId, :opId, :opName, :opIp, :operatorType, :opTime, :sysId, " +
                ":funcId, :eventType, :opType, :opObjType, :opObjId, :relObjType, :relObjId, :serverIp, :serverName, " +
                ":opResult, :logLevel, :logData, :logMemo, :unitId, :opClientInfo, :opOs, :opExplorer, " +
                ":opWay, :sysName, :userType, :insertTime, :provinceId)";
        SqlParameterSource source = new BeanPropertySqlParameterSource(log);
        //拿到新插入的主键值（i）
        KeyHolder keyHolder = new GeneratedKeyHolder();
        int i = namedParameterJdbcTemplate.update(sql, source, keyHolder);
        if(i>0){
            return true;
        }
        return false;
    }
}
